# models.py
from datetime import datetime
from extensions import db  # ← 不再从 app 导入
from datetime import datetime, timedelta

def get_beijing_time():
    return datetime.utcnow() + timedelta(hours=8)

class Account(db.Model):
    __tablename__ = "accounts"
    id = db.Column(db.Integer, primary_key=True)
    student_id = db.Column(db.String(20), unique=True, nullable=False, index=True)
    password_hash = db.Column(db.String(255), nullable=False)
    role = db.Column(db.String(16), default="user")  
    created_at = db.Column(db.DateTime, default=get_beijing_time)
    
    
    
    # 关系：一个用户可以发布多个 Notice
    notices = db.relationship("Notice", backref="author", lazy=True)

    # 关系：一个用户可以写多个评论
    comments = db.relationship("Comment", backref="author", lazy=True)



#用户的详细信息
class Profile(db.Model):
    __tablename__ = "profiles"

    id = db.Column(db.Integer, primary_key=True)

    # 外键指向 accounts 表
    account_id = db.Column(
        db.Integer,
        db.ForeignKey("accounts.id", ondelete="CASCADE"),
        unique=True,
        nullable=False,
        index=True
    )

    # 基本信息字段
    full_name = db.Column(db.String(64))
    birth_date = db.Column(db.String(20))
    address = db.Column(db.String(255))
    phone = db.Column(db.String(32))
    email = db.Column(db.String(120))
    advisor = db.Column(db.String(64))
    degree = db.Column(db.String(32))
    graduation_date = db.Column(db.String(20))
    workplace = db.Column(db.String(128))
    tag = db.Column(db.String(128))
    acad_status = db.Column(db.String(128))
    job_type = db.Column(db.String(128))
    organization = db.Column(db.String(128))
    job_title = db.Column(db.String(64))
    research_direction = db.Column(db.Text)
    introduction = db.Column(db.Text)
    achievements = db.Column(db.Text)
    avatar_url = db.Column(db.String(255))

    created_at = db.Column(db.DateTime, default=get_beijing_time)
    updated_at = db.Column(db.DateTime, default=get_beijing_time, onupdate=get_beijing_time)

    account = db.relationship("Account", backref=db.backref("profile", uselist=False))

   
class Notice(db.Model):
    __tablename__ = "notices"

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255), nullable=False)        # 发布标题
    contact = db.Column(db.String(50), nullable=True)        # 联系方式
    summary = db.Column(db.String(500), nullable=True)       # 简要描述
    content = db.Column(db.Text, nullable=True)              # 详细内容
    created_at = db.Column(db.DateTime, default=get_beijing_time)  # 创建时间
    images = db.Column(db.Text, nullable=True)  # 存JSON字符串 ["url1", "url2"]
    
        # 外键：谁发布了这个 Notice
    account_id = db.Column(db.Integer, db.ForeignKey("accounts.id"), nullable=False)


    # 关系：一个 Notice 可以有多个评论
    comments = db.relationship(
        "Comment",
        backref="notice",
        lazy=True,
        cascade="all, delete-orphan"
    )
    

#需要在app引入
class Comment(db.Model):
    __tablename__ = "comments"

    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.Text, nullable=False)             # 评论内容
    created_at = db.Column(db.DateTime, default=get_beijing_time)

    # 外键：评论属于哪个 Notice
    notice_id = db.Column(
            db.Integer,
            db.ForeignKey("notices.id", ondelete="CASCADE"),
            nullable=False
        )

        # 外键：评论是哪个用户写的
    account_id = db.Column(
            db.Integer,
            db.ForeignKey("accounts.id", ondelete="CASCADE"),
            nullable=False
    )